@extends('layouts.agent')
@section('content')
    <div id="ribbon"><ol class="breadcrumb"><li>内容区</li></ol></div>
    <div class="content_wrapper">
        <aside>
            <header><i class="fa fa-fw fa-file"></i>商品列表</header>
            <section>
                <div class="row">
                    <div class="col-sm-12">
                        <form action="" method="get">
                            <div class="btn-group">
                                <select name="supplier_id" class="form-control">
                                    <option value="">请选择供应商</option>
                                    @foreach($suppliers as $k => $v)
                                        <option value="{{$k}}" {{request('supplier_id') == $k ? 'selected' : ''}}>{{$v}}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="btn-group">
                                <select name="cate_id" class="form-control">
                                    <option value="">请选择分类</option>
                                    @foreach($cateNames as $k => $v)
                                        <option value="{{$k}}" {{request('cate_id') == $k ? 'selected' : ''}}>{{$v}}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="btn-group">
                                <input type="text" class="form-control" placeholder="商品名" name="name" value="{{request('name')}}">
                            </div>
                            <div class="btn-group">
                                <select name="status" class="form-control">
                                    <option value="">请选择状态</option>
                                    @foreach(\App\Model\Goods::$statusLabels as $k => $v)
                                        <option value="{{$k}}" {{isset($params['status']) && $params['status'] !== '' && $params['status'] == $k ? 'selected' : ''}}>{{$v}}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="btn-group">
                                <select name="is_home" class="form-control">
                                    <option value="">请选择首页展示状态</option>
                                    @foreach(\App\Model\Cate::$isHomeLabels as $k => $v)
                                        <option value="{{$k}}" {{isset($params['is_home']) && $params['is_home'] !== '' && $params['is_home'] == $k ? 'selected' : ''}}>{{$v}}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="btn-group">
                                <button class="btn btn-default"><i class="fa fa-filter"></i>查询</button>
                            </div>
                            <div class="btn-group">
                                <a href="{{url('agent/goods/add')}}" class="btn btn-default">
                                    <i class="fa fa-plus"></i>添加
                                </a>
                            </div>
                        </form>
                        <div class="table-responsive" style="margin-top:10px;">
                            <table id="example" class="table table-striped table-bordered table-hover" cellspacing="0"
                                   width="100%">
                                <thead>
                                <tr>
                                    <th>供应商</th>
                                    <th>商品分类</th>
                                    <th>商品名</th>
                                    <th>成本价</th>
                                    <th>原价</th>
                                    <th>现价</th>
                                    <th>序号</th>
                                    <th>首页展示</th>
                                    <th>状态</th>
                                    <th width="150">创建时间</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                    @foreach($list as $value)
                                        <tr>
                                            <td>{{$suppliers[$value['supplier_id']] or ''}}</td>
                                            <td>{{$cateNames[$value['cate_id']] or ''}}</td>
                                            <td>{{$value['name']}}</td>
                                            <td>{{$value['cost']}}</td>
                                            <td>{{$value['origin_price']}}</td>
                                            <td>{{$value['price']}}</td>
                                            <td>{{$value['order_num']}}</td>
                                            <td>{{\App\Model\Goods::$isHomeLabels[$value['is_home']]}}</td>
                                            <td>{{\App\Model\Goods::$statusLabels[$value['status']]}}</td>
                                            <td>{{substr($value['created_at'], 2, 14)}}</td>
                                            <td>
                                                @if($value['status'] == 1)
                                                    <button class="btn btn-warning btn-xs" onclick="changeStatus('{{$value['id']}}', 2)">下架</button>
                                                @else
                                                    <button class="btn btn-success btn-xs" onclick="changeStatus('{{$value['id']}}', 1)">上架</button>
                                                @endif
                                                <a href="{{url('agent/goods/edit?id=' . $value['id'])}}" class="btn btn-success btn-xs">编辑</a>
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                        <nav>{!! $list->appends($params)->render() !!}</nav>
                    </div>
                </div>
            </section>
        </aside>
    </div>
    <script>
        function changeStatus(id, status) {
            var str = status === 1 ? '上架' : '下架';
            if (confirm('确定要' + str + '此商品吗？')) {
                $.get('{{url('agent/goods/changeStatus')}}', {id: id, status: status}, function (res) {
                    if (res.status === 1) {
                        layer.msg(res.info, {icon: res.status}, function () {
                            location.reload();
                        });
                    } else {
                        layer.msg(res.info, {icon: res.status});
                    }
                });
            }
        }
    </script>
@endsection